package com.work.intelligent_transportation2.fragment;

import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.google.gson.Gson;
import com.work.intelligent_transportation2.R;
import com.work.intelligent_transportation2.fragment.RoadSystem.AllSense;
import com.work.intelligent_transportation2.fragment.RoadSystem.HttpUtil;
import com.work.intelligent_transportation2.fragment.RoadSystem.RoadStatus;
import java.util.Timer;
import java.util.TimerTask;

public class RoadSystemFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String mParam1;
    private String mParam2;

    public RoadSystemFragment() {
    }
    public static RoadSystemFragment newInstance(String param1, String param2) {
        RoadSystemFragment fragment = new RoadSystemFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        TextView tvPM2_5, tvTemperature, tvHumidity, tvOneRoad, tvTwoRoad,tvThreeRoad, tvOneBG, tvTwoBG, tvThreeBG;
        String[] tvStrings = {"通畅", "较通畅", "拥挤", "堵塞", "爆表"};
        String[] tvBGs = {"#0ebd12", "#98ed1f", "#ffff01", "#ff0103", "#4c060e"};
        View view = inflater.inflate(R.layout.fragment_road_system, container, false);
        tvPM2_5 = view.findViewById(R.id.road_pm2_5);
        tvTemperature = view.findViewById(R.id.road_temperature);
        tvHumidity = view.findViewById(R.id.road_humidity);
        tvOneRoad = view.findViewById(R.id.road_oneRoad);
        tvTwoRoad = view.findViewById(R.id.road_twoRoad);
        tvThreeRoad = view.findViewById(R.id.road_twoRoad);
        tvOneBG = view.findViewById(R.id.road_BG_oneRoad);
        tvTwoBG = view.findViewById(R.id.road_BG_twoRoad);
        tvThreeBG = view.findViewById(R.id.road_BG_threeRoad);

        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                //空气指数获取和定时刷新
                HttpUtil.asyncRequest(new HttpUtil.AsynCallBack() {
                    @Override
                    public void onFailure(Exception e) { }

                    @Override
                    public void onResponse(String json) {
                        AllSense sense = new Gson().fromJson(json, AllSense.class);
                        tvPM2_5.setText(String.valueOf(sense.getPm25()));
                        tvTemperature.setText(String.valueOf(sense.getTemperature()));
                        tvHumidity.setText(String.valueOf(sense.getHumidity()));
                    }
                }, "get_all_sense", new String[]{"UserName"}, "user1");
                //道路1的信息获取和定时刷新
                HttpUtil.asyncRequest(new HttpUtil.AsynCallBack() {
                    @Override
                    public void onFailure(Exception e) { }

                    @Override
                    public void onResponse(String json) {
                        RoadStatus roadStatus = new Gson().fromJson(json, RoadStatus.class);
                        tvOneRoad.setText("" + tvStrings[roadStatus.getStatus() - 1]);
                        tvOneBG.setBackgroundColor(Color.parseColor(tvBGs[roadStatus.getStatus() - 1]));
                    }
                }, "get_road_status", new String[]{"RoadId", "UserName"}, 1, "User1");
                //道路2的信息获取和定时刷新
                HttpUtil.asyncRequest(new HttpUtil.AsynCallBack() {
                    @Override
                    public void onFailure(Exception e) { }

                    @Override
                    public void onResponse(String json) {
                        RoadStatus roadStatus = new Gson().fromJson(json, RoadStatus.class);
                        tvTwoRoad.setText("" + tvStrings[roadStatus.getStatus() - 1]);
                        tvTwoBG.setBackgroundColor(Color.parseColor(tvBGs[roadStatus.getStatus() - 1]));
                    }
                }, "get_road_status", new String[]{"RoadId", "UserName"}, 1, "User1");
                //道路3的信息获取和定时刷新
                HttpUtil.asyncRequest(new HttpUtil.AsynCallBack() {
                    @Override
                    public void onFailure(Exception e) { }

                    @Override
                    public void onResponse(String json) {
                        RoadStatus roadStatus = new Gson().fromJson(json, RoadStatus.class);
                        tvThreeRoad.setText("" + tvStrings[roadStatus.getStatus() - 1]);
                        tvThreeBG.setBackgroundColor(Color.parseColor(tvBGs[roadStatus.getStatus() - 1]));
                    }
                }, "get_road_status", new String[]{"RoadId", "UserName"}, 1, "User1");
            }
        },1000, 3000);
//        HttpUtil.asyncRequest(new HttpUtil.AsynCallBack() {
//            @Override
//            public void onFailure(Exception e) {
//            }
//
//            @Override
//            public void onResponse(String json) {
//                Toast.makeText(getActivity(), json, Toast.LENGTH_SHORT).show();
//            }
//        }, "get_road_status", new String[]{"RoadId", "UserName"}, 1, "user1");
        return view;
    }
}